<?php

/**
 * This is the model class for table "album".
 *
 * The followings are the available columns in table 'album':
 * @property integer $id
 * @property integer $id_thanh_vien
 * @property string $ten_album
 * @property string $thoi_diem_tao
 * @property string $mo_ta
 *
 * The followings are the available model relations:
 * @property ThanhVien $idThanhVien
 * @property AlbumLike[] $albumLikes
 * @property BinhLuanAlbum[] $binhLuanAlbums
 * @property Hinh[] $hinhs
 */
class Album extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Album the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'album';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_thanh_vien, ten_album, thoi_diem_tao, mo_ta', 'required'),
			array('id_thanh_vien', 'numerical', 'integerOnly'=>true),
			array('ten_album', 'length', 'max'=>100),
			array('mo_ta', 'length', 'max'=>500),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, id_thanh_vien, ten_album, thoi_diem_tao, mo_ta', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idThanhVien' => array(self::BELONGS_TO, 'ThanhVien', 'id_thanh_vien'),
			'albumLikes' => array(self::HAS_MANY, 'AlbumLike', 'id_album'),
			'binhLuanAlbums' => array(self::HAS_MANY, 'BinhLuanAlbum', 'id_album'),
			'hinhs' => array(self::HAS_MANY, 'Hinh', 'id_album'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_thanh_vien' => 'Id Thanh Vien',
			'ten_album' => 'Ten Album',
			'thoi_diem_tao' => 'Thoi Diem Tao',
			'mo_ta' => 'Mo Ta',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
                $criteria->order = 'id desc';
		$criteria->compare('id',$this->id);
		$criteria->compare('id_thanh_vien',$this->id_thanh_vien);
		$criteria->compare('ten_album',$this->ten_album,true);
		$criteria->compare('thoi_diem_tao',$this->thoi_diem_tao,true);
		$criteria->compare('mo_ta',$this->mo_ta,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                    'pagination' => array(
                        'pageSize' => 12,
                        ),
		));
	}
        
        public function searchMyAlbum()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
                
		$criteria->compare('id',$this->id);
		$criteria->compare('ten_album',$this->ten_album,true);
		$criteria->compare('thoi_diem_tao',$this->thoi_diem_tao,true);
		$criteria->compare('mo_ta',$this->mo_ta,true);
                $criteria->addCondition("id_thanh_vien = ".Yii::app()->user->id);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                    'pagination' => array(
                        'pageSize' => 12,
                        ),
		));
	}
        
        public function searchAlbum($key)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
                $connection=Yii::app()->db;    
                $sql = "SELECT DISTINCT id_album FROM hinh WHERE ten_hinh like '".$key."' OR mo_ta like '".$key."' OR id in (SELECT DISTINCT id_hinh from hinh_tag where id_tag in (SELECT id from tag where ten_tag = '".$key."'))";

                $command=$connection->createCommand($sql);
                $dataReader=$command->queryAll();
                
		$criteria=new CDbCriteria;
                
		$criteria->addCondition("ten_album Like '%" . $key . "%'", "OR ");
                $criteria->addCondition("mo_ta Like '%" . $key . "%'", "OR ");
                foreach($dataReader as $row) {
                    $criteria->addCondition("id = " . $row['id_album'] . "", "OR ");
                }
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                        'pagination' => array(
                        'pageSize' => 12,
                        ),
		));
	}
        
        public function layAlbumId()
        {
            if(isset(Yii::app()->user->id)) {
                $connection=Yii::app()->db;
                $sql = "SELECT max(id) as max FROM album WHERE id_thanh_vien = ".Yii::app()->user->id;
                $command=$connection->createCommand($sql);
                $dataReader=$command->query();
                foreach($dataReader as $row) { 
                    return $row['max'];
                }
            } else return 0;
        }
        
        public function searchByDescTimestamp()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
                
                $criteria->order = 'thoi_diem_tao desc';
                
		$criteria->compare('id',$this->id);
		$criteria->compare('id_thanh_vien',$this->id_thanh_vien);
		$criteria->compare('ten_album',$this->ten_album,true);
		$criteria->compare('thoi_diem_tao',$this->thoi_diem_tao,true);
		$criteria->compare('mo_ta',$this->mo_ta,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                    'pagination' => array(
                        'pageSize' => 12,
                        ),
		));
	}
}